/**
 * @author Tobias Boehm
 *
 * 19.05.2011
 */
package org.anddev.andengine.extension.tbsengine.message.client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import org.anddev.andengine.extension.tbsengine.message.ICopyable;
import org.anddev.andengine.extension.tbsengine.player.BasePlayer;

/**
 * This client message should be used to join or leave a game server. It
 * contains a player ID and a flag to indicate whether or not the player wants to 
 * join the server, the {@link #mPlayerName} and optional a {@link #mPassword}.
 * 
 * 
 * @author Tobias Boehm
 *
 */
public class ClientJoinOrLeaveServerRequestMessage extends AbsClientMessage implements ICopyable<ClientJoinOrLeaveServerRequestMessage>{

	private boolean mJoinServer = false;
	private String mPlayerName = "";
	private String mPassword = "";

	
	/* (non-Javadoc)
	 * @see org.anddev.andengine.extension.multiplayer.protocol.adt.message.IMessage#getFlag()
	 */
	@Override
	public short getFlag() {
		
		return FLAG_CLIENT_JOIN_OR_LEAVE_SERVER_REQUEST;
	}

	/* (non-Javadoc)
	 * @see org.anddev.andengine.extension.tbsengine.multiplayer.client.messages.AbsClientMessage#onReadTransmissionClientData(java.io.DataInputStream)
	 */
	@Override
	protected void onReadTransmissionClientData(DataInputStream pDataInputStream)
			throws IOException {
		
		this.mJoinServer = pDataInputStream.readBoolean();
		
		this.mPlayerName = pDataInputStream.readUTF();
		
		this.mPassword = pDataInputStream.readUTF();
	}

	/* (non-Javadoc)
	 * @see org.anddev.andengine.extension.tbsengine.multiplayer.client.messages.AbsClientMessage#onWriteTransmissionClientData(java.io.DataOutputStream)
	 */
	@Override
	protected void onWriteTransmissionClientData(DataOutputStream pDataOutputStream) throws IOException {
		
		pDataOutputStream.writeBoolean(this.mJoinServer);
		
		pDataOutputStream.writeUTF(this.mPlayerName);
		
		pDataOutputStream.writeUTF(this.mPassword);

	}

	/**
	 * @return the mJoinServer
	 */
	public boolean isJoiningServer() {
		return mJoinServer;
	}

	/**
	 * @param mJoinServer the mJoinServer to set
	 */
	public void setJoinServer(boolean mJoinServer) {
		this.mJoinServer = mJoinServer;
	}

	/**
	 * @return the mPlayerName
	 */
	public String getPlayerName() {
		return mPlayerName;
	}
	
	@Override
	public void setPlayer(BasePlayer pPlayer) {
		super.setPlayer(pPlayer);
		
		this.mPlayerName = pPlayer.getName();
	}

	/**
	 * @return the mPassword
	 */
	public String getPassword() {
		return mPassword;
	}

	/**
	 * @param mPassword the mPassword to set
	 */
	public void setPassword(String mPassword) {
		this.mPassword = mPassword;
	}

	@Override
	public ClientJoinOrLeaveServerRequestMessage copy() {
		
		final ClientJoinOrLeaveServerRequestMessage copy = new ClientJoinOrLeaveServerRequestMessage();
		
		copy.mJoinServer = this.mJoinServer;
		copy.mPassword = this.mPassword;
		copy.mPlayerID = this.mPlayerID;
		copy.mPlayerName = this.mPlayerName;
		
		return copy;
	}

}
